#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

"""
@author: JoeyZhou
@Tool: PyCharm
@Date: 2020/2/19 12:12 AM
@desc: 截图+裁剪 封装
"""

import os
import shutil
from time import sleep
from os import path
from PIL import Image
from base.get_newest_file import GetNewestFile
from selenium import webdriver

parent_path = path.dirname(path.dirname(__file__))  # 获得当前目录的父级目录
reports_path = parent_path + "/reports/"
screenshot = parent_path + "/screenshot/"


class ModifyImageBase:
    def screenshot_web(self):
        """
        截取网页图片
        :return:
        """
        new_report = GetNewestFile()
        url = new_report.get_newest_report()
        picName = url.replace(".html", ".png")
        brower = webdriver.PhantomJS()
        brower.get(url)
        brower.maximize_window()
        sleep(3)
        brower.save_screenshot(picName)
        shutil.move(picName, screenshot)    # 移动截图到screenshot文件夹中
        brower.close()

    def crop_web(self):
        """
        裁剪网页图片
        :return:
        """
        # 未完成！！！
        # for img in os.listdir('./screen_shot'):
        #     if img.endswith('.png'):
        #         print('%s裁剪中。。' % img)
        #         im = Image.open('./screen_shot/%s' % img)
        #         x = 755
        #         y = 162
        #         w = 383
        #         h = 346
        #         region = im.crop((x, y, x + w, y + h))
        #         region.save("./screenshot_final/%s" % img)


if __name__ == '__main__':
    modifyImg = ModifyImageBase()
    modifyImg.screenshot_web()
